﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LIM.model;
using System.Reflection;

namespace LIM.controller
{
    public class Thanh_CRMBaoGiaChiTietController : BaseController
    {
        public Thanh_CRMBaoGiaChiTietController()
        {
        }

        public tbl_CRM_BaoGia GetBaoGia(int pRowID)
        {
            if (pRowID == -1)
                return LIMS_Entities.tbl_CRM_BaoGia.FirstOrDefault();
            return LIMS_Entities.tbl_CRM_BaoGia.Where(p => p.RowID == pRowID).FirstOrDefault();
        }

        public IQueryable<Thanh_ChiTieuComboBox> GetsChiTieuComboBox()
        {
            return LIMS_Entities.tbl_DanhMuc_ChiTieu.Select(p => new Thanh_ChiTieuComboBox { RowID = p.RowID, MaChiTieu = p.MaChiTieu, TenChiTieu = p.TenGoi, LoaiMau = p.tbl_DanhMuc_LoaiMau.TenGoi });
        }

        public IQueryable<Thanh_TinhTrangComboBox> GetsTinhTrangComboBox()
        {
            DanhMucHeThongController oDanhMucHeThongController = new DanhMucHeThongController();
            return oDanhMucHeThongController.LoadDataChild("TinhTrangBaoGia").AsQueryable().Select(p => new Thanh_TinhTrangComboBox()
                {
                    RowID = p.RowID,
                    MaDanhMuc = p.MaDanhMuc,
                    TenGoi = p.TenGoi
                });
        }

        public Thanh_ChiTieuKiemNghiem GetsChiTieuGridView(int pRowIDChiTieu)
        {
            DateTime CurrenDate = base.GetCurrentDateTimeFromDatabaseServer().Date.AddDays(1);
            return LIMS_Entities.tbl_DanhMuc_ChiTieu.Where(p => p.RowID == pRowIDChiTieu).Select(p => new Thanh_ChiTieuKiemNghiem
            {
                //RowID = p.RowID,
                //MaChiTieu = p.RowID,
                //TenChiTieu = p.RowID,
                //STT = index + 1,
                //RowIDLoaiMau = p.tbl_DanhMuc_LoaiMau.RowID,
                LoaiMau = p.tbl_DanhMuc_LoaiMau.TenGoi,
                PhuongPhap = p.PhuongPhap,
                SoLuong = 1,
                DonGia = p.tbl_DanhMuc_ChiTieu_BangGia.Where(g => g.NgayHieuLuc.Value < CurrenDate).FirstOrDefault().Gia.Value,
                //ThanhTien = p.tbl_DanhMuc_ChiTieu_BangGia.Where(g => g.NgayHieuLuc.Value.Date <= CurrenDate).First().Gia.Value
            }).FirstOrDefault();
        }

        public IQueryable<Thanh_KhachHang> GetsKhachHang()
        {
            return LIMS_Entities.tbl_CRM_KhachHang.Where(p => p.LaKhachHangKiemNghiem.HasValue && p.LaKhachHangKiemNghiem.Value && p.TinhTrangKhachHang == "TinhTrangKhachHang_DangHoatDongBinhThuong")
                .Select(p => new Thanh_KhachHang { RowID = p.RowID, MaKhachHang = p.MaKhachHang, TenGoi = p.TenGoi, TenGoiNgan = p.TenGoiNgan, DiaChi = p.DiaChi, SoDienThoai = p.SoDienThoai, SoFax = p.SoFax });
        }

        public IQueryable<Thanh_LienHe> GetsLienHe(int pRowIDKhachHang)
        {
            var query = LIMS_Entities.tbl_CRM_KhachHang.Where(p => p.RowID == pRowIDKhachHang).FirstOrDefault();
            if (query != null && query.tbl_CRM_LienHeKhachHang.Count > 0)
            {
                return query.tbl_CRM_LienHeKhachHang.AsQueryable()
                .Select(p => new Thanh_LienHe { RowID = p.RowID, EmailCaNhan = p.tbl_CRM_LienHe.EmailCaNhan, EmailCongTy = p.Email, HoTen = p.tbl_CRM_LienHe.HoTen, SoDiDong = p.tbl_CRM_LienHe.SoDiDong });
            }
            return null;
        }

        public bool SaveBaoGia(Thanh_BaoGia pThanh_BaoGia, List<Thanh_ChiTieuKiemNghiem> pChiTieuKiemNghiems)
        {
            bool result = false;
            DateTime currentDate = base.GetCurrentDateTimeFromDatabaseServer();
            tbl_CRM_BaoGia otbl_CRM_BaoGia = LIMS_Entities.tbl_CRM_BaoGia.Where(p => p.RowID == pThanh_BaoGia.RowID).FirstOrDefault();
            if (otbl_CRM_BaoGia == null)
            {
                otbl_CRM_BaoGia = new tbl_CRM_BaoGia();
                pThanh_BaoGia.NgayTao = currentDate;
                pThanh_BaoGia.NguoiTao = InitUserInfos.UserName;

                LIMS_Entities.tbl_CRM_BaoGia.AddObject(otbl_CRM_BaoGia);
                ////\\ Dung.
                //LIMS_Entities.Refresh(System.Data.Objects.RefreshMode.StoreWins, otbl_CRM_BaoGia);
            }
            else
            {
                pThanh_BaoGia.NgayTao = otbl_CRM_BaoGia.NgayTao.Value;
                pThanh_BaoGia.NguoiTao = otbl_CRM_BaoGia.NguoiTao;
            }
            // Set values.
            pThanh_BaoGia.NgayCapNhat = currentDate;
            pThanh_BaoGia.NguoiCapNhat = InitUserInfos.UserName;
            Util.CopyValues(pThanh_BaoGia, otbl_CRM_BaoGia, true, "RowID");
            List<tbl_CRM_BaoGia_ChiTieuKiemNghiem> lstBGKMOld = otbl_CRM_BaoGia.tbl_CRM_BaoGia_ChiTieuKiemNghiem.ToList();
            otbl_CRM_BaoGia.tbl_CRM_BaoGia_ChiTieuKiemNghiem.Clear();
            foreach (var item in pChiTieuKiemNghiems)
            {
                tbl_CRM_BaoGia_ChiTieuKiemNghiem otblChiTieuKiemNghiem = lstBGKMOld.Where(p => p.RowID == item.RowID).FirstOrDefault();
                if (otblChiTieuKiemNghiem == null)
                {
                    otblChiTieuKiemNghiem = new tbl_CRM_BaoGia_ChiTieuKiemNghiem();
                    item.NgayTao = currentDate;
                    item.NguoiTao = InitUserInfos.UserName;
                }
                else
                {
                    item.NgayTao = otblChiTieuKiemNghiem.NgayTao.Value;
                    item.NguoiTao = otblChiTieuKiemNghiem.NguoiTao;
                }
                item.NgayCapNhat = currentDate;
                item.NguoiCapNhat = InitUserInfos.UserName;

                Util.CopyValues(item, otblChiTieuKiemNghiem, true, "RowID");
                otbl_CRM_BaoGia.tbl_CRM_BaoGia_ChiTieuKiemNghiem.Add(otblChiTieuKiemNghiem);
            }
            //try
            {
                LIMS_Entities.SaveChanges();
                result = true;
            }
            //catch (Exception ex)
            {
            }
            return result;
        }

        public bool DeleteBaoGia(int pRowIDBaoGia)
        {
            tbl_CRM_BaoGia otbl_CRM_BaoGia = LIMS_Entities.tbl_CRM_BaoGia.Where(p => p.RowID == pRowIDBaoGia).FirstOrDefault();
            if (otbl_CRM_BaoGia != null)
            {
                for (int i = 0; i < otbl_CRM_BaoGia.tbl_CRM_BaoGia_ChiTieuKiemNghiem.Count; i++)
                {
                    tbl_CRM_BaoGia_ChiTieuKiemNghiem ctkm = otbl_CRM_BaoGia.tbl_CRM_BaoGia_ChiTieuKiemNghiem.ElementAt(i--);
                    otbl_CRM_BaoGia.tbl_CRM_BaoGia_ChiTieuKiemNghiem.Remove(ctkm);
                    LIMS_Entities.DeleteObject(ctkm);
                }
                LIMS_Entities.tbl_CRM_BaoGia.DeleteObject(otbl_CRM_BaoGia);
                LIMS_Entities.SaveChanges();
                return true;
            }
            return false;
        }

    }

    public class Util
    {
        public static void CopyValues(object pSource, object pDestination, bool pIsSourcePropeties)
        {
            Type tSource = pSource.GetType();
            Type tDestination = pDestination.GetType();
            PropertyInfo[] tPros = null;
            // Get pros of src.
            if (pIsSourcePropeties)
                tPros = tSource.GetProperties();
            // Get pros of des.
            else
                tPros = tDestination.GetProperties();
            PropertyInfo pPro = null;
            foreach (PropertyInfo item in tPros)
            {
                // Get pro of des.
                if (pIsSourcePropeties)
                    pPro = tDestination.GetProperty(item.Name);
                // Get pro of src.
                else
                    pPro = tSource.GetProperty(item.Name);
                // Check pro remain.
                if (pPro != null)
                {
                    if (pIsSourcePropeties)
                        pPro.SetValue(pDestination, item.GetValue(pSource, null), null);
                    else
                        item.SetValue(pDestination, pPro.GetValue(pSource, null), null);
                }
            }
        }

        public static void CopyValues(object pSource, object pDestination, bool pIsSourcePropeties, params string[] pExcludePropertyNames)
        {
            Type tSource = pSource.GetType();
            Type tDestination = pDestination.GetType();
            PropertyInfo[] tPros = null;
            // Get pros of src.
            if (pIsSourcePropeties)
                tPros = tSource.GetProperties();
            // Get pros of des.
            else
                tPros = tDestination.GetProperties();
            PropertyInfo pPro = null;
            foreach (PropertyInfo item in tPros)
            {
                if (pExcludePropertyNames.Contains(item.Name))
                    continue;
                // Get pro of des.
                if (pIsSourcePropeties)
                    pPro = tDestination.GetProperty(item.Name);
                // Get pro of src.
                else
                    pPro = tSource.GetProperty(item.Name);
                // Check pro remain.
                if (pPro != null)
                {
                    if (pIsSourcePropeties)
                        pPro.SetValue(pDestination, item.GetValue(pSource, null), null);
                    else
                        item.SetValue(pDestination, pPro.GetValue(pSource, null), null);
                }
            }
        }

        public static void CopyValues(object pSource, object pDestination, params string[] pPropertyNames)
        {
            Type tSource = pSource.GetType();
            Type tDestination = pDestination.GetType();
            foreach (string item in pPropertyNames)
            {
                PropertyInfo sourcePro = tSource.GetProperty(item);
                PropertyInfo desPro = tDestination.GetProperty(item);
                if (sourcePro != null && desPro != null)
                {
                    desPro.SetValue(pDestination, sourcePro.GetValue(pSource, null), null);
                }
            }
        }

    }

    public class Thanh_Base
    {
        public DateTime NgayTao { get; set; }
        public DateTime NgayCapNhat { get; set; }
        public string NguoiTao { get; set; }
        public string NguoiCapNhat { get; set; }
    }

    public class Thanh_ChiTieuComboBox
    {
        public int RowID { get; set; }
        public string MaChiTieu { get; set; }
        public string TenChiTieu { get; set; }
        public string LoaiMau { get; set; }
    }

    public class Thanh_TinhTrangComboBox
    {
        public int RowID { get; set; }
        public string MaDanhMuc { get; set; }
        public string TenGoi { get; set; }
    }

    public class Thanh_ChiTieuKiemNghiem : Thanh_Base
    {
        public decimal DonGia { get; set; }
        public decimal ThanhTien { get; set; }
        public int RowID { get; set; }
        public int RowIDChiTieu { get; set; }
        int _MaChiTieu;
        public int MaChiTieu { get { return _MaChiTieu; } set { _MaChiTieu = value; RowIDChiTieu = value; } }
        public int TenChiTieu { get; set; }
        private int _STT;
        public int STT { get { return _STT; } set { _STT = value; SoThuTu = value; } }
        public int SoThuTu { get; set; }
        public int SoLuong { get; set; }
        public int RowIDLoaiMau { get; set; }
        public string LoaiMau { get; set; }
        public string PhuongPhap { get; set; }
    }

    public class Thanh_KhachHang
    {
        public int RowID { get; set; }
        public string MaKhachHang { get; set; }
        public string TenGoi { get; set; }
        public string TenGoiNgan { get; set; }
        public string DiaChi { get; set; }
        public string SoDienThoai { get; set; }
        public string SoFax { get; set; }
    }

    public class Thanh_LienHe
    {
        public int RowID { get; set; }
        public string HoTen { get; set; }
        public string SoDiDong { get; set; }
        public string ChucVu { get; set; }
        public string EmailCaNhan { get; set; }
        public string EmailCongTy { get; set; }

    }

    public class Thanh_BaoGia : Thanh_Base
    {
        public int RowID { get; set; }
        public int RowIDKhachHang { get; set; }
        public int RowIDLienHe { get; set; }
        public string MaBaoGia { get; set; }
        public string TinhTrang { get; set; }
        public string TenKhachHang { get; set; }
        public string DiaChi { get; set; }
        public string SoDienThoai { get; set; }
        public string SoFax { get; set; }
        public string HoTenNguoiLienHe { get; set; }
        public string SoDiDong { get; set; }
        public string Email { get; set; }
        public string GhiChu { get; set; }
        public DateTime NgayGui { get; set; }
        public DateTime ThoiGianHieuLuc_Tu { get; set; }
        public DateTime ThoiGianHieuLuc_Den { get; set; }
    }

    public class Thanh_HopDong : Thanh_Base
    {
        public int RowID { get; set; }
        public int RowIDKhachHang { get; set; }
        public int RowIDLienHe { get; set; }
        public string MaHopDong { get; set; }
        public string TinhTrang { get; set; }
        public string TenKhachHang { get; set; }
        public string DiaChi { get; set; }
        public string SoDienThoai { get; set; }
        public string SoFax { get; set; }
        public string HoTenNguoiDaiDien { get; set; }
        public string MaSoThue { get; set; }
        public string TaiKhoanNganHang { get; set; }
        public string ChucVu { get; set; }
        //public string SoDiDong { get; set; }
        //public string Email { get; set; }
        public string SoTang { get; set; }
        public string SoGiam { get; set; }
        public string GhiChu { get; set; }
        public DateTime NgayKy { get; set; }
        public DateTime ThoiGianHieuLuc_Tu { get; set; }
        public DateTime ThoiGianHieuLuc_Den { get; set; }
    }

    public class Thanh_DieuKhoanThanhToan : Thanh_Base
    {
        public decimal SoTien { get; set; }
        public int RowID { get; set; }
        private int _STT;
        public int STT { get { return _STT; } set { _STT = value; SoThuTu = value; } }
        public int SoThuTu { get; set; }
        public int GiaTriHopDong { get; set; }
        public string MoTa { get; set; }
        public bool DaChuyenThanhNoPhaiThu { get; set; }
    }

}
